package com.hyou.demos.chain.context;

import java.util.LinkedList;

import com.hyou.demos.chain.handler.Handler;

public class DefaultHandlerContext implements HandlerContext {
    
    private LinkedList<Handler> handlerLst;
    
    public DefaultHandlerContext() {
        this.handlerLst = new LinkedList<>();
    }
    
    @Override
    public void addLast(Handler handler) {
        handlerLst.addLast(handler);
    }
    
    @Override
    public void fireInvokeRequest(Object paramObj) {
        
        for (Handler handler : handlerLst) {
            try {
                boolean goon = handler.invokeRequest(paramObj);
                
                if (!goon) {
                    break;
                }
                
            } catch (Exception e) {
                boolean goon = handler.exceptionCaught(e);
                
                if (!goon) {
                    break;
                }
            }
        }
        
    }

}
